package com.boco.pciv.service.project.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.boco.pciv.SysProperties;
import com.boco.pciv.core.entity.PageResultBase;
import com.boco.pciv.core.entity.SearchCriteria;
import com.boco.pciv.core.exception.PcivException;
import com.boco.pciv.core.service.PcivBaseServiceImpl;
//import com.boco.pciv.entity.project.FreeHouseInfo;
import com.boco.pciv.core.util.SearchUtil;
import com.boco.pciv.entity.project.*;
import com.boco.pciv.entity.sys.UserLogInfo;
import com.boco.pciv.mapper.project.IProjectMapper;
import com.boco.pciv.mapper.project.IPropertyMapper;
import com.boco.pciv.service.project.IProjectService;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ProjectServiceImpl extends PcivBaseServiceImpl<IProjectMapper,ProjectInfo,Integer> implements IProjectService {
    //logger
    private static final Logger _logger = LoggerFactory.getLogger(ProjectServiceImpl.class);
    @Autowired
    SysProperties sysConfig;
    
    @Override
    public PageResultBase getPage(SearchCriteria cond){
        if (cond==null){
            cond = new SearchCriteria();
        }
        List<ProjectInfo> list = mapper.queryList(cond.getQueryMap());
        for (ProjectInfo info:list){
            info.setUserType(cond.getQueryMap().get("userType").toString());
        }
        return SearchUtil.parse(list);
    }

    public ProjectInfo getProject(String projectId) {
        Map<String, Object> map = new HashMap();
        map.put("projectId", projectId);
        ProjectInfo info = mapper.queryOneByMap(map);
        return info;
    }


    public void addProject(ProjectInfo info) {
        mapper.insert(info);
    }

    public void editProject(ProjectInfo info) {
        mapper.update(info);
    }

    public void insertRecord(ProjectInfo info) {
        mapper.insertRecord(info);
    }

    public void deleteProject(String projectId,String status) {
        Map<String, Object> map = new HashMap();
        map.put("projectId",projectId);
        if("del".equals(status)){
            map.put("status",0);
        }
        else{
            map.put("status",1);
        }
        mapper.deleteByMap(map);
    }

    public int CheckProject(ProjectInfo info) {
        Map<String, Object> map = new HashMap();
        map.put("projectName", info.getProjectname());
        map.put("licno", info.getLicno());
        return mapper.queryList(map).size();
    }

    public List<ProjectInfo> getSelectByProject(String companyid) {
        Map<String, Object> map = new HashMap();
        map.put("companyid", companyid);
        List<ProjectInfo> list=  mapper.getSelectByProject(map);
        return list;
    }

    public List<ProjectInfo> getSelectByProjecttype() {
        List<ProjectInfo> list=  mapper.getSelectByProjecttype();
        return list;
    }

    public List<ProjectInfo> getSelectByProjectstatus() {
        List<ProjectInfo> list=  mapper.getSelectByProjectstatus();
        return list;
    }

    public User getDept(User info) {
        User value=  mapper.getDept(info);
        return value;
    }

    public PageResultBase banzulist(SearchCriteria cond) {
        if (cond == null) {
            cond = new SearchCriteria();
        }
        List<banzuinfo> list = mapper.banzulist(cond.getQueryMap());
        //预警判断
        Integer mo = DateUtil.dayOfMonth(DateUtil.date());
        if (mo >= Integer.parseInt(sysConfig.getAlarmDay())) {
            cond.putCriteria("isAlarm","1,2");
            cond.putCriteria("alarmYear",DateUtil.year(DateUtil.date()));
            cond.putCriteria("alarmMonth",(DateUtil.month(DateUtil.date())+1));
            List<banzuinfo> data = mapper.banzusendlist(cond.getQueryMap());
            for (banzuinfo node : list) {
                long count=data.stream().filter(x->x.getBanzu_name().equals(node.getBanzu_name())).count();
                if(count<=0){
                    node.setAlram("1");
                }
            }
        }
        return SearchUtil.parse(list);
    }

    public void banzuadd(banzuinfo info) {
        String[] persons=info.getNumber().split(",");
        info.setNumber(String.valueOf(persons.length));
        mapper.banzuinsert(info);
        //保存班组和劳务工关系
        bzps_resInsert(info,persons,"add");
    }

    //新增班组和务工人员关系
    public void  bzps_resInsert(banzuinfo info,String[] persons,String type){
        int bz_id=0;
        if(type.equals("add")){
            bz_id=mapper.getBZID(info.getBanzu_name());
        }
        else{
            bz_id=Integer.parseInt(info.getId());
        }
        //删除原来班组关系
        mapper.deleteBzps_res(bz_id);
        Map<String,Integer> map=new HashMap<>();
        map.put("bz_id",bz_id);
        for (String p:persons
        ) {
            map.put("person_id",Integer.parseInt(p));
            mapper.bzps_resInsert(map);
        }
    }
    public String getBzps_res(String bzid){
        List<String> res=mapper.getBzps_res(bzid);
        return res.stream().collect(Collectors.joining(","));
    }
    public void editbanzu(banzuinfo info) {
        String[] persons=info.getNumber().split(",");
        info.setNumber(String.valueOf(persons.length));
        mapper.editbanzu(info);
        //保存班组和劳务工关系
        bzps_resInsert(info,persons,"edit");
    }

    public banzuinfo getBanzuProject(String Id) {
        Map<String, Object> map = new HashMap();
        map.put("Id", Id);
        banzuinfo info = mapper.getBanzuProject(map);
        info.setNumber(getBzps_res(Id));
        return info;
    }

    public void deletebanzu(String Id,String status) {
        Map<String, Object> map = new HashMap();
        map.put("Id",Id);
        if("del".equals(status)){
            map.put("status",0);
            mapper.deleteBzps_res(Integer.parseInt(Id));
        }
        else{
            map.put("status",1);
        }
        mapper.deletebanzu(map);
    }
    public String banzuSend(banzuinfo info) {
        Map<String, Object> map=new HashMap<>();
        map.put("banzu_name",info.getBanzu_name());
        map.put("alarmYear",info.getYear());
        map.put("alarmMonth",info.getMonth());
        map.put("isAlarm",2);
        List<banzuinfo> list = mapper.banzusendlist(map);
        //判断是否有已经填报了的、审核通过的
        if(list.size()>0){
            return "该班组在所选月份已填报过工资！";
        }
        else{
            mapper.banzuSend(info);
        }
        return "";
    }

    public PageResultBase banzusendlist(SearchCriteria cond){
        if (cond==null){
            cond = new SearchCriteria();
        }
        List<banzuinfo> list = mapper.banzusendlist(cond.getQueryMap());
        return SearchUtil.parse(list);
    }

    public void deletebanzusend(String Id){
        Map<String, Object> map = new HashMap();
        map.put("Id",Id);
        mapper.deletebanzusend(map);
    }
    public banzuinfo sendinfo(String Id) {
        Map<String, Object> map = new HashMap();
        map.put("Id", Id);
        banzuinfo info = mapper.sendinfo(map);
        return info;
    }

    public void upsendstatus(banzuinfo info) {
        mapper.upsendstatus(info);
    }
    public String pro_com_name(SearchCriteria info){
        return mapper.pro_com_name(info.getQueryMap());
    }

    public void addPerson(PersonInfo info) {
        mapper.addPerson(info);
    }
    public void personEdit(PersonInfo info) {
        mapper.personEdit(info);
    }
    public void deletePerson(String id) {
        Map<String, Object> map = new HashMap();
        map.put("Id",id);
        mapper.deletePerson(map);
    }
    public PageResultBase personlist(SearchCriteria cond){
        if (cond==null){
            cond = new SearchCriteria();
        }
        List<PersonInfo> list = mapper.personlist(cond.getQueryMap());
        return SearchUtil.parse(list);
    }
    public PersonInfo personInfo(String Id) {
        Map<String, Object> map = new HashMap();
        map.put("Id", Id);
        PersonInfo info = mapper.personInfo(map);
        return info;
    }
    public List<PersonInfo> getperson(String licno) {
        Map<String, Object> map = new HashMap();
        map.put("licno", licno);
        List<PersonInfo> list=  mapper.getperson(map);
        return list;
    }
    public void addPersonPay(PersonPay info){
        String[] persons=info.getSfz_card().split(",");
        for(String p:persons){
             info.setSfz_card(p);
            mapper.addPersonPay(info);
        }
    }
}
